package listbyorder.access001_100.test60;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/2 9:15
 */
public class Solution1 {

    // 自己写的方法
    // 首先将所有全排列的组合全部拟写出来
    // 然后将选择第k个元素返回即可
    // 超时报错
    public String getPermutation(int n, int k) {
        List<String> res = new ArrayList<>();
        boolean[] hasUse = new boolean[n];
        dfs(res, hasUse, "", n);
        return res.get(k - 1);
    }

    private void dfs(List<String> res, boolean[] hasUse, String path, int n) {
        if (path.length() == n) {
            res.add(new String(path));
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!hasUse[i]) {
                hasUse[i] = true;
                path = path + "" + (i + 1);
                dfs(res, hasUse, path, n);
                hasUse[i] = false;
                path = path.substring(0, path.length() - 1);
            }
        }
    }

}
